package net.suncaper.mvc.web.controller;

import net.suncaper.mvc.common.framework.simplemvc.RequestMapping;
import net.suncaper.mvc.common.utils.EntityConverter;
import net.suncaper.mvc.common.utils.JSONUtil;
import net.suncaper.mvc.common.utils.Result;
import net.suncaper.mvc.model.domain.Message;
import net.suncaper.mvc.model.vo.MessageVO;
import net.suncaper.mvc.service.IMessageService;
import net.suncaper.mvc.service.impl.MessageServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 消息控制器
 */
@RequestMapping("/api/message")
public class MessageController {
    
    private final IMessageService messageService = new MessageServiceImpl();
    
    /**
     * 获取用户消息列表
     * 请求参数：userId
     */
    @RequestMapping("/listByUser.do")
    public void listMessagesByUser(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
            
            String userId = request.getParameter("userId");
            if (userId == null || userId.isEmpty()) {
                JSONUtil.printByJSON(response, Result.error("用户ID不能为空"));
                return;
            }
            
            List<Message> messages = messageService.getMessagesByUserId(userId);
            List<MessageVO> messageVOList = EntityConverter.convertToMessageVOList(messages);
            JSONUtil.printByJSON(response, Result.success(messageVOList));
        } catch (Exception e) {
            JSONUtil.printByJSON(response, Result.error(e.getMessage()));
        }
    }
    
    /**
     * 根据类型获取用户消息列表
     * 请求参数：userId、type
     */
    @RequestMapping("/listByType.do")
    public void listMessagesByType(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
            
            String userId = request.getParameter("userId");
            String type = request.getParameter("type");
            
            if (userId == null || userId.isEmpty()) {
                JSONUtil.printByJSON(response, Result.error("用户ID不能为空"));
                return;
            }
            
            if (type == null || type.isEmpty()) {
                JSONUtil.printByJSON(response, Result.error("消息类型不能为空"));
                return;
            }
            
            List<Message> messages = messageService.getMessagesByUserIdAndType(userId, type);
            List<MessageVO> messageVOList = EntityConverter.convertToMessageVOList(messages);
            JSONUtil.printByJSON(response, Result.success(messageVOList));
        } catch (Exception e) {
            JSONUtil.printByJSON(response, Result.error(e.getMessage()));
        }
    }
    
    /**
     * 获取用户未读消息列表
     * 请求参数：userId
     */
    @RequestMapping("/listUnread.do")
    public void listUnreadMessages(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
            
            String userId = request.getParameter("userId");
            if (userId == null || userId.isEmpty()) {
                JSONUtil.printByJSON(response, Result.error("用户ID不能为空"));
                return;
            }
            
            List<Message> messages = messageService.getUnreadMessagesByUserId(userId);
            List<MessageVO> messageVOList = EntityConverter.convertToMessageVOList(messages);
            JSONUtil.printByJSON(response, Result.success(messageVOList));
        } catch (Exception e) {
            JSONUtil.printByJSON(response, Result.error(e.getMessage()));
        }
    }
    
    /**
     * 获取未读消息数量
     * 请求参数：userId
     */
    @RequestMapping("/unreadCount.do")
    public void getUnreadMessageCount(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
            
            String userId = request.getParameter("userId");
            if (userId == null || userId.isEmpty()) {
                JSONUtil.printByJSON(response, Result.error("用户ID不能为空"));
                return;
            }
            
            int count = messageService.getUnreadMessageCount(userId);
            JSONUtil.printByJSON(response, Result.success(count));
        } catch (Exception e) {
            JSONUtil.printByJSON(response, Result.error(e.getMessage()));
        }
    }
    
    /**
     * 标记消息为已读
     * 请求参数：messageId
     */
    @RequestMapping("/read.do")
    public void markMessageAsRead(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
            
            String messageIdStr = request.getParameter("messageId");
            if (messageIdStr == null || messageIdStr.isEmpty()) {
                JSONUtil.printByJSON(response, Result.error("消息ID不能为空"));
                return;
            }
            
            Long messageId = Long.parseLong(messageIdStr);
            boolean result = messageService.markMessageAsRead(messageId);
            JSONUtil.printByJSON(response, Result.success(result));
        } catch (Exception e) {
            JSONUtil.printByJSON(response, Result.error(e.getMessage()));
        }
    }
    
    /**
     * 标记所有消息为已读
     * 请求参数：userId
     */
    @RequestMapping("/readAll.do")
    public void markAllMessagesAsRead(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
            
            String userId = request.getParameter("userId");
            if (userId == null || userId.isEmpty()) {
                JSONUtil.printByJSON(response, Result.error("用户ID不能为空"));
                return;
            }
            
            boolean result = messageService.markAllMessagesAsRead(userId);
            JSONUtil.printByJSON(response, Result.success(result));
        } catch (Exception e) {
            JSONUtil.printByJSON(response, Result.error(e.getMessage()));
        }
    }
    
    /**
     * 删除消息
     * 请求参数：messageId
     */
    @RequestMapping("/delete.do")
    public void deleteMessage(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
            
            String messageIdStr = request.getParameter("messageId");
            if (messageIdStr == null || messageIdStr.isEmpty()) {
                JSONUtil.printByJSON(response, Result.error("消息ID不能为空"));
                return;
            }
            
            Long messageId = Long.parseLong(messageIdStr);
            boolean result = messageService.deleteMessageById(messageId);
            JSONUtil.printByJSON(response, Result.success(result));
        } catch (Exception e) {
            JSONUtil.printByJSON(response, Result.error(e.getMessage()));
        }
    }
    
    /**
     * 删除所有消息
     * 请求参数：userId
     */
    @RequestMapping("/deleteAll.do")
    public void deleteAllMessages(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
            
            String userId = request.getParameter("userId");
            if (userId == null || userId.isEmpty()) {
                JSONUtil.printByJSON(response, Result.error("用户ID不能为空"));
                return;
            }
            
            boolean result = messageService.deleteMessagesByUserId(userId);
            JSONUtil.printByJSON(response, Result.success(result));
        } catch (Exception e) {
            JSONUtil.printByJSON(response, Result.error(e.getMessage()));
        }
    }
} 